package com.drug.web.controller.wms;

import com.drug.common.annotation.Log;
import com.drug.common.core.controller.BaseController;
import com.drug.common.core.domain.AjaxResult;
import com.drug.common.core.domain.model.LoginUser;
import com.drug.common.core.page.TableDataInfo;
import com.drug.common.enums.BusinessType;
import com.drug.common.utils.SecurityUtils;
import com.drug.common.utils.poi.ExcelUtil;
import com.drug.wms.domain.BusTransferGoods;
import com.drug.wms.mapper.BusTransferGoodsMapper;
import com.drug.wms.page.BusTransferGoodsPage;
import com.drug.wms.page.BusTransferGoodsQuery;
import com.drug.wms.service.IBusTransferGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品调货单Controller
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@RestController
@RequestMapping("/wms/trans")
public class BusTransferGoodsController extends BaseController {
    @Autowired
    private IBusTransferGoodsService busTransferGoodsService;
    @Autowired
    private BusTransferGoodsMapper busTransferGoodsMapper;

    /**
     * 查询商品调货单列表
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusTransferGoodsQuery busTransferGoodsQuery) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        BusTransferGoods busTransferGoods = new BusTransferGoods();
        BeanUtils.copyProperties(busTransferGoodsQuery, busTransferGoods);
        //审核类型（"":"所有"，“1”：”我申请“，“2”：“我审核”）
        if ("1".equals(busTransferGoodsQuery.getApproveType())) {
            busTransferGoods.setEntryUserId(loginUser.getUserId());
        } else if ("2".equals(busTransferGoodsQuery.getApproveType())) {
            busTransferGoods.setTransferDeptId(String.valueOf(loginUser.getDeptId()));
        }
        startPage();
        List<BusTransferGoods> list = busTransferGoodsService.selectBusTransferGoodsList(busTransferGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品调货单列表
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "商品调货单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusTransferGoods busTransferGoods) {
        List<BusTransferGoods> list = busTransferGoodsService.selectBusTransferGoodsList(busTransferGoods);
        ExcelUtil<BusTransferGoods> util = new ExcelUtil<BusTransferGoods>(BusTransferGoods.class);
        return util.exportExcel(list, "商品调货单数据");
    }

    /**
     * 获取商品调货单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(busTransferGoodsService.selectBusTransferGoodsById(id));
    }

    /**
     * 新增商品调货单
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品调货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusTransferGoodsPage busTransferGoodsPage) {
        return toAjax(busTransferGoodsService.insertBusTransferGoods(busTransferGoodsPage));
    }

    /**
     * 修改商品调货单
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品调货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusTransferGoodsPage busTransferGoodsPage) {
        return toAjax(busTransferGoodsService.updateBusTransferGoods(busTransferGoodsPage));
    }

    /**
     * 删除商品调货单
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "商品调货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(busTransferGoodsService.deleteBusTransferGoodsByIds(ids));
    }

    /**
     * 审核当前调货单
     *
     * @param id            当前调货单id
     * @param result        审核结果
     * @param checkedReason 审核原因
     * @return
     */
    @GetMapping("approve")
    public AjaxResult approve(String id, String result, String checkedReason) {
        BusTransferGoods busTransferGoods = new BusTransferGoods();
        busTransferGoods.setId(id);
        busTransferGoods.setCheckedReason(checkedReason);
        if ("1".equals(result)) {
            busTransferGoods.setInputStatus("2");
            busTransferGoodsService.approveTransfer(id);
            //TODO 对应加减库存
        } else {
            busTransferGoods.setInputStatus("3");
        }
        busTransferGoodsMapper.updateBusTransferGoods(busTransferGoods);
        return toAjax(true);
    }

    /**
     * 撤销当前调货单
     *
     * @param id
     * @return
     */
    @GetMapping("revoke")
    public AjaxResult revoke(String id) {
        //判断当前是否可以撤销
        BusTransferGoods busTransferGoods = new BusTransferGoods();
        busTransferGoods.setId(id);
        busTransferGoods.setInputStatus("1");
        busTransferGoodsMapper.updateBusTransferGoods(busTransferGoods);
        return toAjax(true);
    }

}